package com.czl.module_storehouse.activity.borrow.details;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.czl.base.data.bean.tengyun.BorrowBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.http.HttpResponse;
import com.czl.module_base.mvp.presenter.InjectPresenter;
import com.czl.module_base.utils.ParcelHelper;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.base.BaseOperationActivity;
import com.czl.module_storehouse.adapter.BorrowAddPostAdapter;
import com.czl.module_storehouse.bean.BorrowInfoBean;
import com.czl.module_storehouse.databinding.ActivityReceiveAddBinding;
import com.czl.module_storehouse.databinding.FooterCommonRemarkBinding;
import com.czl.module_storehouse.databinding.HeaderBorrowDetailsBinding;
import com.czl.module_storehouse.event.BorrowEvent;
import com.czl.module_storehouse.event.DetailsPostSuccessEvent;
import com.czl.module_storehouse.mvp.presenter.BorrowPresenter;
import com.czl.module_storehouse.mvp.view.BorrowView;
import com.jeremyliao.liveeventbus.LiveEventBus;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class BorrowDetailsPostActivity extends BaseOperationActivity<ActivityReceiveAddBinding> implements BorrowView {

    private final BorrowInfoBean mBorrowInfoBean = new BorrowInfoBean();
    private BorrowAddPostAdapter mAdapter;
    private HeaderBorrowDetailsBinding mHeaderBinding;
    private FooterCommonRemarkBinding mFooterBinding;
    @InjectPresenter
    BorrowPresenter mBorrowPresenter;

    @Override
    protected ActivityReceiveAddBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityReceiveAddBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mHeaderBinding = HeaderBorrowDetailsBinding.inflate(getLayoutInflater());
        mFooterBinding = FooterCommonRemarkBinding.inflate(getLayoutInflater());
        super.initData(savedInstanceState);
        mToolBinding.toolbarContentTitle.setText("物品借用");
        binding.btnSure.setEnabled(false);
        binding.btnSure.setText("借用出库");
        binding.btnSure.setOnClickListener(this);
        binding.recyclerView.setAdapter(getAdapter());
        mAdapter.addHeaderView(mHeaderBinding.getRoot());
        mAdapter.addFooterView(mFooterBinding.getRoot());
    }

    @Override
    protected BaseQuickAdapter<SortBean, ?> getAdapter() {
        if (mAdapter == null) {
            mAdapter = new BorrowAddPostAdapter(R.layout.item_borrow_add_post, new ArrayList<>());
            mAdapter.setOnItemChildClickListener((adapter, view, position) ->
                    startActivity(position - mAdapter.getHeaderLayoutCount()));
        }
        return mAdapter;
    }

    @Override
    protected String getOperationTitle() {
        return "借用";
    }

    @Override
    protected Button getButton() {
        return binding.btnSure;
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(BorrowEvent event) {
        BorrowBean borrowBean = event.getBorrowBean();
        initHeaderView(borrowBean);
        //拷贝数据
        BorrowBean copy = ParcelHelper.copy(borrowBean);
        List<SortBean> sortList = copy.getSortList();
        if (sortList == null) {
            return;
        }
        getAdapter().setList(sortList);
    }

    @SuppressLint("SetTextI18n")
    private void initHeaderView(BorrowBean borrowBean) {
        mHeaderBinding.clHeaderInfo.ivHeader.setImageResource(R.mipmap.icon_lingyong_head);
        if (borrowBean == null) {
            return;
        }
        mBorrowInfoBean.setBorrowBean(borrowBean);
        mHeaderBinding.clHeaderInfo.tvHeaderTitle.setText(borrowBean.getBorrowName());
        mHeaderBinding.clHeaderInfo.tvHeaderDate.setText(borrowBean.getBorrowDate());
        mHeaderBinding.clHeaderInfo.tvHeaderCode.setText("借用单：" + (borrowBean.getBorrowCode() == null ? "" : borrowBean.getBorrowCode()));
        mHeaderBinding.tvHeaderDepartment.setText("借用部门：" + (borrowBean.getBorrowPersonDepartment() == null ? "" : borrowBean.getBorrowPersonDepartment()));
        mHeaderBinding.tvHeaderDesc.setText("借用原因：" + (borrowBean.getApplyComment() == null ? "" : borrowBean.getApplyComment()));
        mHeaderBinding.tvHeaderCompany.setText("借用公司：" + (borrowBean.getBorrowCompanyName() == null ? "" : borrowBean.getBorrowCompanyName()));
        mHeaderBinding.tvReturnDate.setText("拟还日期：" + (borrowBean.getMoReturnDate() == null ? "" : borrowBean.getMoReturnDate()));
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        submit();
    }

    @Override
    protected void submit() {
        //申请借用出库
        List<SortBean> newList = getNewList();
        mBorrowInfoBean.setSortList(newList);
        String remark = mFooterBinding.etRemark.getText().toString();
        mBorrowInfoBean.setBorrowComment(remark);
        if (mBorrowPresenter != null) {
            mBorrowPresenter.addBorrowInfo(mBorrowInfoBean);
        }
    }

    @Override
    public <T> void showHttpResponse(HttpResponse<T> httpResponse) {
        super.showHttpResponse(httpResponse);
        if (BorrowPresenter.TAG_BORROW_ADD_INFO.equals(httpResponse.getRequestTag())) {
            showToast("借用成功");
            LiveEventBus.get(DetailsPostSuccessEvent.class).post(new DetailsPostSuccessEvent());
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mHeaderBinding != null) {
            mHeaderBinding.unbind();
        }
        if (mFooterBinding != null) {
            mFooterBinding.unbind();
        }
    }
}
